/**
 * 
 */
package com.eagle.coders.common.ui.form.renderer;

import java.util.List;
import java.util.Set;
import java.util.SortedMap;

import org.dom4j.Element;

import com.eagle.coders.common.form.annotations.type.UIComponentType;
import com.eagle.coders.common.ui.form.renderer.cache.UIDomainPropertyBean;

/**
 * @author Anees
 *
 */
public class UiFormDecorator {

	private Element propertiesElement;
	private Element bindingsElement;
	
	private static UiFormDecorator instance;
	
	public UiFormDecorator(){
	}
	
	public static UiFormDecorator getInstance(){
		if(null == instance)
			instance = new UiFormDecorator();
		return instance;
	}
	
	/**
	 * 
	 * @param uiDomainPropertyList
	 */
	public void decorator(final Element domainElement, final SortedMap<Integer, List<UIDomainPropertyBean>> propertyHolderListMap){
		
		propertiesElement = domainElement.addElement("properties");
		bindingsElement = domainElement.addElement("property-bindings");
		
		Set<Integer> rows = propertyHolderListMap.keySet();
		
		for(Integer row : rows){
			
			Element rowNode = propertiesElement.addElement("row");
			
			rowNode.addAttribute("rowNumber", row.toString());
			
			List<UIDomainPropertyBean> propertyColumnList = propertyHolderListMap.get(row);
			
			buildColumnNodes(propertyColumnList, rowNode);
		}
		
	}
	
	/**
	 * 
	 * @param propertyColumnList
	 * @param rowNumber
	 */
	private void buildColumnNodes(final List<UIDomainPropertyBean> propertyColumnList, Element rowNumber){
		
		/* it will just create the xml for columns with the proper info for the component type, position in row 
		 * and validation properties 
		 */
		int colPosition = 1;
		
		for(UIDomainPropertyBean propertyHolder : propertyColumnList ){
			
			Element columnNode = rowNumber.addElement("column");
			columnNode.addAttribute("colPosition", String.valueOf(colPosition));
			
			Element widgetNode = columnNode.addElement("widget");

			String pLabel= propertyHolder.getUiPropertyLabel();
			Element labelNode = widgetNode.addElement("node-label");
			labelNode.addAttribute("label", pLabel);
			
			//TODO: for temporary purpose === just to check whether it works fine or not
			String resourceKey = propertyHolder.getResourceKey();
			
			if(null != resourceKey && !"".equals(resourceKey)){
				labelNode.addAttribute("resourceKey", resourceKey);
			}else{
				labelNode.addAttribute("resourceKey", "");
			}
			
			widgetNode.addAttribute("property", propertyHolder.getUiProperty());
			/* Also adding to the widget Node avoid the problem in Rendering as sometime the renderer will take propeerty */
			if(null != resourceKey && !"".equals(resourceKey)){
				widgetNode.addAttribute("resourceKey", resourceKey);
			}else{
				widgetNode.addAttribute("resourceKey", "");
			}
			
//			String propertyName = propertyHolder.getDomainProperty();
//			String propertyType = propertyHolder.getUiPropertyType();
			if(null != propertyHolder.getUiPropertyType()){
				generateWidgetNode(propertyHolder, widgetNode);
				colPosition++;
			}
			
		}
	}
	
	/**
	 * 
	 * @param propertyHolder
	 * @param widgetElement
	 */
	private void generateWidgetNode(UIDomainPropertyBean propertyHolder, Element widgetElement){
		
		if(propertyHolder.getUiPropertyType().equals(UIComponentType.COMPNENT_TEXTFIELD.getValue())){
			
			UiWidgets.createWidgetNode(widgetElement, "text", propertyHolder);
			
		}else if(propertyHolder.getUiPropertyType().equals(UIComponentType.COMPONENT_FMT_NUM.getValue())){
//			TODO: have to work on Formatting and Masking At this POINT
			UiWidgets.createWidgetNode(widgetElement, "text", propertyHolder);
			
		}else if (propertyHolder.getUiPropertyType().equals(UIComponentType.COMPNENT_PASSWORD.getValue())){
			UiWidgets.createWidgetNode(widgetElement, "password", propertyHolder);
			
		}else if(propertyHolder.getUiPropertyType().equals(UIComponentType.COMPONENT_TEXTAREA.getValue())){
			
			UiWidgets.createTextAreaNode(widgetElement, "textArea", propertyHolder);
			
		}else if(propertyHolder.getUiPropertyType().equals(UIComponentType.COMPNENT_TABLE.getValue())){
		
			if(null != propertyHolder.getRelationshipClassProperty()){
				UiWidgets.createTableNode(widgetElement, "table", propertyHolder.getRelationshipClassProperty(), propertyHolder);
			}
			
		}else if(propertyHolder.getUiPropertyType().equals(UIComponentType.COMPONENT_CHECKBOX.getValue())){
			
			UiWidgets.createWidgetNode(widgetElement, "checkBox", null);
			
		}else if(propertyHolder.getUiPropertyType().equals(UIComponentType.COMPNENT_COMBOBOX.getValue())){
			
//			UiWidgets.createWidgetNode(widgetElement, "combobox", null);
			UiWidgets.createComboboxNode(widgetElement, "combobox", propertyHolder);
			
		}else if(propertyHolder.getUiPropertyType().equals(UIComponentType.COMPNENT_LIST.getValue())){
			
			UiWidgets.createWidgetNode(widgetElement, "list", null);
			
		}else if( propertyHolder.getUiPropertyType().equals(UIComponentType.COMPNENT_RADIOBUTTON.getValue())){
			
			UiWidgets.createRadioButtonNode(propertyHolder.getDomainPropertyHolderList(), widgetElement, "radioButton");
			
		}else if(propertyHolder.getUiPropertyType().equals(UIComponentType.COMPONENT_FMT_TXT.getValue())){
			System.out.println("[Renderer][UiFormDecorator] ********************** ========= FORMATTED ========= ********************");
			
//			TODO: Formate definition of the JFormateTdTextField is left
			UiWidgets.createWidgetNode(widgetElement, "text", propertyHolder);
			
		}else if(propertyHolder.getUiPropertyType().equals(UIComponentType.COMPONENT_DATE.getValue())){
			
			UiWidgets.createWidgetNode(widgetElement, "date", null);
			
		}else if (propertyHolder.getUiPropertyType().equals(UIComponentType.COMPNENT_PANEL.getValue())){
			
			UiWidgets.createWidgetNode(widgetElement, "text", null);//Temporary purpose - I will change this latter
			
		}
		
//		TODO: ========================== IMPORTANT and NEED TO BE VARIFIED ================================
		if(null != propertyHolder.getParentProperty() && null != propertyHolder.getDependentProperty()){
			
			Element binding = bindingsElement.addElement("binding");
			Element parent = binding.addElement("parent");
			parent.addText(propertyHolder.getParentProperty());
			
			Element child = binding.addElement("child");
			child.addText(propertyHolder.getDependentProperty());
		}
	}
}